// @ts-ignore
import type {RouteRecordRaw} from "vue-router";
import {createRouter, createWebHistory} from 'vue-router'
import FriendCircle from "../views/FriendCircle.vue";
import Information from "../views/Information.vue";
import Interlligence from "../views/Interlligence.vue";
import M03 from "../views/M03.vue";
import Recommend from "../views/Recommend.vue";
import Find from "../views/Find.vue";
import Service from "../views/Service.vue";
import XiaoPeng from "../views/XiaoPeng.vue"
import Shop from "../views/Shop.vue"
import My from "../views/My.vue"
import Recommend2 from "@/views/Recommend2.vue";
import Search from "@/components/Search/Search.vue"
import Setting from "@/views/Setting.vue";
// @ts-ignore
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
// @ts-ignore
import {usePermissStore} from "@/stores/permiss.ts";
//自己人用的,没有什么兼容性问题
//移动端用户项目也不需要怎么考虑兼容性
//用户端PC项目flex?
// @ts-ignore
const router: RouteRecordRaw[] = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: '/',
            redirect: '/login',
        },
        {
            path: '/find',
            name: 'find',
            component: Find,
            children: [
                {
                    path: '/find',
                    redirect: 'find/recommend',
                },
                {
                    path: '/find/recommend',
                    name: 'recommend',
                    component: Recommend,
                    meta: {
                        title: '推荐',
                        noAuth: true//不需要授权
                    }
                },
                {
                    path: '/find/friendCircle',
                    name: 'friendCircle',
                    meta: {
                        title: '朋友圈',
                        permiss: '31',
                        noAuth: true
                    },
                    component: FriendCircle,
                },
                {
                    path: '/find/m03',
                    name: 'm03',
                    component: M03,
                    meta: {
                        title: 'm03',
                       noAuth: true//不需要授权/登录
                    }
                },
                {
                    path: '/find/interlligence',
                    name: '智能',
                    meta: {
                        title: '智能',
                        permiss: '11',
                        noAuth: true
                    },
                    component: Interlligence
                },
                {
                    path: '/find/information',
                    name: 'information',
                    meta: {
                        title: '咨询',
                        permiss: '11',
                        noAuth: true
                    },
                    component: Information
                }
            ],
            meta: {
                title: '发现',
            },
        },
        {
            path: '/login',
            name: 'login',
            component: () => import('@/views/Login.vue'),
            meta: {
                title: '登录',
                noAuth: true,
            }
        },
        {
            path: '/register',
            name: 'register',
            component: () => import('@/views/Register.vue'),
            meta: {
                title: '注册',
                noAuth: true
            }
        },
        {
            path: '/service',
            name: 'service',
            component: Service,
            meta: {
                title: '服务',
                permiss: '31',
                 noAuth:true
            },
        },
        {
            path: '/xiaopeng',
            name: 'xiaopeng',
            component: XiaoPeng,
            meta: {
                title: '小鹏',
                permiss: '11',
                 noAuth:true
            },
        },
        {
            path: '/shop',
            name: 'shop',
            component: Shop,
            children: [
                {
                    path: '/shop',
                    redirect: '/shop/recommend2'
                },
                {
                    path: '/shop/recommend2',
                    name: 'recommend2',
                    component: Recommend2,
                    meta: {
                        title: '商店',
                        permiss: '11',
                        noAuth: true
                    },
                },
                {
                    path: '/shop/search',
                    name: 'search',
                    component: Search
                }

            ],
            meta: {
                title: '商店',
                permiss: '11',
                 noAuth:true
            },
        },
        {
            path: '/my',
            name: 'my',
            component: My,
            meta: {
                title: '我的',
                permiss: '11',
                 noAuth:true
            },
        },
        { path: '/setting',
            name: 'setting',
            component: Setting,
            meta: {
                title: '设置',
                permiss: '11',
                noAuth:true
            },
        },
        {
            path: '/403',
            name: '403',
            meta: {
                title: '403 Forbidden',
                noAuth: true
            },
            component: () => import('@/views/403.vue')
        },
        {
            path: '/404',
            name: '404',

            meta: {
                title: '404 NotFound',
                noAuth: true,
            },
            component: () => import('@/views/404.vue')
        },
        {
            path: '/:path(.*)',
            redirect: '/404'
        },
    ]
})
const whitePath = ['/login', '/register']
// @ts-ignore
router.beforeEach((to, from, next) => {
    NProgress.start();
    const role = localStorage.getItem('useInfo');//登录？
    const permissStore = usePermissStore()
    //鉴权 分角色 403 404
    document.title = to.meta.title as string;
    // if (!whitePath.includes(to.path)) {
    //     //判断本地有无用户数据
    //     if (!localStorage.getItem('userInfo')) {
    //         // @ts-ignore
    //         router.push('/login')
    //         return
    //     }
    //     next()
    //     return
    // }

    //我没有登录,并且将要跳转页面需要登录/授权
    if (!role && to.meta.noAuth !== true) {
        next('./login')
        //登陆了 访问的页面要鉴权,通过typeof 规避排除未登录且不需要登录(login本身)
        //登陆了 访问的页面要登录授权   未登录 访问的页面不需要登录授权  登陆了访问的界面不需要登录授权
    } else if (
        //这个页面要检验permiss
       role && typeof to.meta.permiss === 'string' && !permissStore.key.includes(to.meta.permiss)
    ) {
        next('/403')
    }
    else if (role && permissStore.key.includes(to.meta.permiss)){//未登录且不需要登录 要鉴权且有权限的   直接放行
        next();
    }else {
        next()
    }
})
// @ts-ignore
router.afterEach((to, from, next) => {
    NProgress.done()
})
export default router
